package com.xucy.springboot.annotationaop.AspectAop1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;

/**
 * @Author xucy
 * @Date 2019-08-05 11:18
 * @Description
 **/

@Aspect
@Component
public class AspectAop1  {

    public static ThreadLocal<Long> threadLocal=new ThreadLocal<>();

    @Pointcut("execution(public * com.xucy.springboot.annotationaop.AspectAop1..*.*(..))")
    public  void pointCut(){

    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) throws IllegalAccessException{
        String tableName = "";
        StringBuilder sb = new StringBuilder();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Object[] object = joinPoint.getArgs();
        Parameter[] parameters = method.getParameters();
        for (Parameter parameter : parameters) {
            if (parameter.isAnnotationPresent(SqlAnnotation.class)) {
                System.out.println("有SqlAnnotation 注解");
                Annotation[] annotations = parameter.getDeclaredAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof SqlAnnotation) {
                        tableName = ((SqlAnnotation) annotation).value();
                        System.out.println(tableName);
                    }
                }

                //输出的是参数的名称 batchGrant
                System.out.println(parameter.getName());

                //输出的是参数的类名和类的字段
                Field[] fields = parameter.getType().getDeclaredFields();
                for (int i = 0; i < fields.length; i++) {
                    Field field = fields[i];

                    if (field.isAnnotationPresent(WhereSql.class)) {
                        String name = (field.getAnnotation(WhereSql.class)).value();
                        field.setAccessible(true);
                        String value = "";
                        List list = null;
                        if (field.get(object[0]) instanceof String) {
                            value = (String) field.get(object[0]);
                        } else if (field.get(object[0]) instanceof List) {
                            {
                                list = (List) field.get(object[0]);
                            }
                        }
                        if (name.length() == 0) {
                            name = field.getName();
                        }
                        if (value .length()>0) {
                            sb.append(name + "=" + value);
                            System.out.println(name + "=" + value);

                            if (i != (fields.length - 1)) {
                                sb.append(" and ");
                            }
                        }
                    }
                }

            }
        }
        if (sb.length() > 4) {
            System.out.println(tableName + "=" + sb.substring(0, sb.length() - 4));
        } else {
            System.out.println(tableName + "=" + sb);
        }
        threadLocal.set(System.currentTimeMillis());
    }

    @After("pointCut()")
    public void after() throws IllegalAccessException {

            Long start = threadLocal.get();

            Long end = System.currentTimeMillis();

            System.out.println("共消耗时间是：" + (end - start));
        }
}
